package com.chucang.shucang.common.base.utils;

import com.chucang.shucang.common.base.constant.CommonConstant;
import lombok.*;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * @author flitsneak
 * @email flitsneak@gmail.com
 * @date 2022/9/16 10:48
 * @description 响应类
 */
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class R<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    @Getter
    @Setter
    private boolean flag;

    @Getter
    @Setter
    private int code;

    @Getter
    @Setter
    private String msg;

    @Getter
    @Setter
    private T data;

    public static <T> R<T> ok() {
        return restResult(true, null, CommonConstant.SUCCESS, null);
    }

    public static <T> R<T> ok(T data) {
        return restResult(true, data, CommonConstant.SUCCESS, null);
    }

    public static <T> R<T> ok(T data, String msg) {
        return restResult(true, data, CommonConstant.SUCCESS, msg);
    }

    public static <T> R<T> failed() {
        return restResult(false, null, CommonConstant.FAIL, null);
    }

    public static <T> R<T> failed(String msg) {
        return restResult(false, null, CommonConstant.FAIL, msg);
    }

    public static <T> R<T> failed(T data) {
        return restResult(false, data, CommonConstant.FAIL, null);
    }

    public static <T> R<T> failed(int code, String msg) {
        return restResult(false, null, code, msg);
    }

    public static <T> R<T> failed(T data, int code, String msg) {
        return restResult(false, data, code, msg);
    }

    public static <T> R<T> failed(boolean flag, T data, String msg) {
        return restResult(flag, data, CommonConstant.FAIL, msg);
    }

    public static <T> R<T> restResult(boolean flag, T data, int code, String msg) {
        R<T> apiResult = new R<>();
        apiResult.setFlag(flag);
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }

}
